package com.itguigu.fruit.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class demo04 {
    public static void main(String[] args) throws Exception{
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //链接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");
        //编写SQL语句
        String sql="select * from t_fruit where fname like ?";
        //psmt
        PreparedStatement psmt=conn.prepareStatement(sql);
psmt.setString(1,"苹果");
        //执行查询，rs
        ResultSet rs = psmt.executeQuery();

        if (rs.next()){
            //整理数据类型
            Integer fid = rs.getInt(1);
            String fname = rs.getString(2);
            Integer price = rs.getInt(3);
            Integer fcount = rs.getInt(4);
            String remark = rs.getString("remark");

            //输出
            System.out.println(fid+','+fname+','+price+','+fcount+','+remark);
        }

        //释放资源
        rs.close();
        psmt.close();
        conn.close();

    }
}
